const express = require("express")
const path = require("path")

const app = express()

const home = require("./routes/home")
const article = require("./routes/article")

// 静态目录挂载
let publicPath = path.join(__dirname, "./galcg_pro/build")
// console.log(publicPath)

// 中间件
app.use(express.static(publicPath))
app.use(express.json())
app.use(express.urlencoded({extended: false}))


// cors 跨域处理
app.use('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With")
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS")
    res.header("X-Powered-By",' 3.2.1')
    if(req.method=="OPTIONS"){
        res.send(200)
    }
    else {
        next()
    }
})

// 路由数据
app.use("/home", home)
app.use("/article", article)


// 开启8080端口
app.listen(8080, () => {
    console.log("8080端口已开启~~~")
})